﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Udp;
namespace app.scode.redo
{
    public partial class Form1 : Form
    {
        private bool isFirstLand = true;
        private bool isFirstSea = true;
        private bool isRedraw = false;
        private float tmpLheight = 0f;
        private bool isBlank = false;
        private Bitmap bit = new Bitmap(640, 400);
        private Bitmap bitbuffer = new Bitmap(640, 400); 
        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            UDPSocket udp = new UDPSocket(8088, 8888, udp_SOCKETEventArrive);
            udp.StartRecvThreadListener();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.AllPaintingInWmPaint, true);
        }


        void udp_SOCKETEventArrive(byte[] sReceived)
        {
            scode data = new scode();
            #region setmove
            if (data.getdata(sReceived,true))
            {
                if (data.dcFlag == (short)GLOBAL.Flag.off)
                {
                    //listBox1.Items.Add("大车开始移动");
                    if (GLOBAL.MOVING.IS_MOVING)
                    {
                        GLOBAL.MOVING.RUNNING_Delay = data.dataTime - GLOBAL.MOVING.Begin_DateTime;
                    }
                    else
                    {
                        GLOBAL.MOVING.RUNNING_Delay = 0;
                        GLOBAL.MOVING.Begin_DateTime = data.dataTime;
                        GLOBAL.MOVING.IS_MOVING = true;
                    }
                }
                else
                {
                    GLOBAL.MOVING.reset();
                }
            }
            textBox11.Text = GLOBAL.MOVING.RUNNING_Delay.ToString();
            #endregion                                        
            float tmp = 0f;
            if (data.lheight >= 10000)
            {
                tmp = data.lheight / 1000.0f;
                //tmp = tmpLheight;
            }
            else
            {
                tmp = data.lheight / 100.0f;
                tmpLheight = tmp;
            }
            if (data.ljjkbFlag == (short)GLOBAL.Flag.off)
            {
                if (!isBlank)
                {
                    DrawPic(data.off / 10, (int)tmp * 4, Color.Salmon);
                }
                DrawPicBlank(data.off / 10, (int)tmp * 4);
                isBlank = true;
            }
            else
            {

                DrawPicEx(data.off / 10, (int)tmp * 4, Color.LightBlue);
                isBlank = false;
            }

            setText(data);
            Thread_getLand(data);
            Thread_getSea(data);

            //data.Check();
            //throw new NotImplementedException();
        }
        #region settext
        /// <summary>
        /// 实现设置text，代码由c++更改得到
        /// </summary>
        /// <param name="data"></param>
        private void setText(scode data)
        {

            string strTemp = string.Empty;

            if (data.off == GLOBAL.CONST.LONG_ERROR_DATA)
                strTemp = "无数据";
            else
                strTemp = string.Format("{0}米", (data.off / 100.0f).ToString());
            textBox1.Text = strTemp;

            strTemp = "";
            if (data.sheight == GLOBAL.CONST.LONG_ERROR_DATA)
                strTemp = "无数据";
            else
                strTemp = string.Format("{0}米", (data.sheight / 100.0f).ToString());
            //m_editSHeight.SetWindowText(strTemp);		//海测举升高度
            textBox2.Text = strTemp;

            strTemp = "";
            if (data.lheight == GLOBAL.CONST.LONG_ERROR_DATA)
                strTemp = "无数据";
            else
            {
                if (data.lheight >= 10000)
                {
                    strTemp = string.Format("{0}米",
                        (data.lheight / 1000.0f).ToString()
                    );
                }
                else
                {
                    strTemp = string.Format("{0}米",
                        (data.lheight / 100.0f).ToString()
                    );
                }
            }

            textBox4.Text = strTemp;
            //MessageBox.Show(data.lheight.ToString().Substring(0, 2));
            //吊具间距
            strTemp = "";
            if (data.lspace == GLOBAL.CONST.LONG_ERROR_DATA)
            {
                strTemp = "无效数据";
            }
            else
            {

                strTemp = string.Format("{0}米", (data.lspace / 1000.0f).ToString());
            }
            textBox5.Text = strTemp;

            //吊具间距
            strTemp = "";
            if (data.rspace == GLOBAL.CONST.LONG_ERROR_DATA)
            {
                strTemp = "无效数据";
            }
            else
            {
                strTemp = string.Format("{0}米", (data.rspace / 1000.0f).ToString());
            }
            textBox9.Text = strTemp;


            //开闭锁
            strTemp = "";
            if (data.sjjkbFlag == GLOBAL.CONST.SHORT_ERROR_DATA)
                strTemp = "无数据";
            else if (data.sjjkbFlag == 1)
                strTemp = "闭锁";
            else if (data.sjjkbFlag == 0)
                strTemp = "开锁";

            textBox3.Text = strTemp;

            strTemp = "";
            if (data.ljjkbFlag == GLOBAL.CONST.SHORT_ERROR_DATA)
                strTemp = "无数据";
            else if (data.ljjkbFlag == 1)
                strTemp = "闭锁";
            else if (data.ljjkbFlag == 0)
                strTemp = "开锁";
            textBox7.Text = strTemp;

            strTemp = "";
            if (data.ljjkbFlag == GLOBAL.CONST.SHORT_ERROR_DATA)
                strTemp = "无数据";
            else if (data.ljjkbFlag == 1)
                strTemp = "漂浮";
            else if (data.ljjkbFlag == 0)
                strTemp = "着陆";
            textBox12.Text = strTemp;

            strTemp = "";
            if (data.dcFlag == GLOBAL.CONST.SHORT_ERROR_DATA)
                strTemp = "无数据";
            else if (data.dcFlag == 1)
                strTemp = "停止";
            else if (data.dcFlag == 0)
                strTemp = "移动中";
            textBox10.Text = strTemp;

            //箱型
            strTemp = "";
            if (data.ssize == GLOBAL.CONST.SHORT_ERROR_DATA)
            {
                strTemp = "无数据";
            }
            else
            {
                strTemp = string.Format("{0}", data.ssize);
            }

            textBox8.Text = strTemp;

            strTemp = "";
            if (data.lsize == GLOBAL.CONST.SHORT_ERROR_DATA)
            {
                strTemp = "无数据";
            }
            else
            {
                strTemp = string.Format("{0}", data.lsize);
            }
            textBox6.Text = strTemp;
        }
        #endregion
        #region thread
        private void Thread_getLand(scode data)
        {
            //on->off
            if (isFirstLand)
            {
                isFirstLand = !isFirstLand;
                GLOBAL.Landpredata = data;
                return;
            }
            if (GLOBAL.Landpredata.ljjkbFlag != data.ljjkbFlag)
            {
                switch (GLOBAL.Landpredata.ljjkbFlag)
                {
                    case (short)GLOBAL.Flag.on:
                        //off->on
                        if (GLOBAL.LandIsOff)
                        {
                            if (data.off < GLOBAL.CONST.DISTANCE_IN)
                            {
                                if (data.dataTime - GLOBAL.Landpredata.dataTime < GLOBAL.CONST.BREAK_TIMER)
                                {
                                    //大车子程行进超过一段距离
                                    if (GLOBAL.MOVING.RUNNING_Delay > GLOBAL.CONST.BREAK_TIMER_CRAIN)
                                    {
                                        //refresh
                                        isRedraw = true;
                                    }
                                    else
                                    {
                                        //draw
                                        isRedraw = false;
                                        listBox1.Items.Add(string.Format("{0},{1}", data.off.ToString(), data.lheight.ToString()));
                                        //float tmp = float.Parse(data.lheight.ToString().Substring(0, 2) + "." + data.lheight.ToString().Substring(2));
                                        float tmp = 0f;
                                        //舍弃>10000
                                        if (data.lheight >= 10000)
                                        {
                                            tmp = data.lheight / 1000.0f;
                                            //tmp = tmpLheight;
                                        }
                                        else
                                        {
                                            tmp = data.lheight / 100.0f;
                                            tmpLheight = tmp;
                                        }
                                        DrawPic(data.off / 10, (int)tmp * 4, Color.OrangeRed);
                                    }
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                return;
                            }
                            GLOBAL.LandIsOff = !GLOBAL.LandIsOff;
                        }
                        break;
                    case (short)GLOBAL.Flag.off:
                        //on->off
                        if (data.off < GLOBAL.CONST.DISTANCE_OUT)
                        {
                            GLOBAL.LandIsOff = false;
                        }
                        else
                        {
                            //大铁板
                            GLOBAL.LandIsOff = true;
                        }
                        break;
                }
            }
            GLOBAL.Landpredata = data;
        }
        private void Thread_getSea(scode data)
        {
            //on->off
            if (isFirstSea)
            {
                isFirstSea = !isFirstSea;
                GLOBAL.Seapredata = data;
                return;
            }
            if (GLOBAL.Seapredata.sjjkbFlag != data.sjjkbFlag)
            {
                //label10.Text = "发生改变！";
                switch (GLOBAL.Seapredata.sjjkbFlag)
                {
                    case (short)GLOBAL.Flag.off:
                        //off->on
                        if (GLOBAL.SeaIsOff)
                        {
                            if (data.off < GLOBAL.CONST.DISTANCE_IN)
                            {
                                if (data.dataTime - GLOBAL.Seapredata.dataTime < GLOBAL.CONST.BREAK_TIMER)
                                {
                                    //大车子程行进超过一段距离
                                    if (GLOBAL.MOVING.RUNNING_Delay > GLOBAL.CONST.BREAK_TIMER_CRAIN)
                                    {
                                        //refresh
                                    }
                                    else
                                    {
                                        //draw
                                    }
                                }
                                else
                                {
                                    return;
                                }
                            }
                            else
                            {
                                return;
                            }
                            GLOBAL.SeaIsOff = !GLOBAL.SeaIsOff;
                        }
                        break;
                    case (short)GLOBAL.Flag.on:
                        //on->off
                        if (data.off > GLOBAL.CONST.DISTANCE_OUT)
                        {
                            return;
                        }
                        else
                        {
                            //大铁板
                            GLOBAL.SeaIsOff = true;
                        }
                        break;
                }
            }
            GLOBAL.Seapredata = data;
        }
        #endregion
        #region drawpic
        private void DrawPic(int x, int y,Color color)
        {

            SolidBrush blueBrush = new SolidBrush(color);
            Graphics e = Graphics.FromImage(bit);
            x -= 450;
            y = pictureBox2.Height - y - 50;
            //if(isRedraw){e.Clear(Color.White);}
            //e.Clear(pictureBox1.BackColor);
            //y = pictureBox1.Height - y - 40;
            e.DrawRectangle(new Pen(Color.Red, 2.0f), new Rectangle(x, y, GLOBAL.drawEvents.CrainWidth, GLOBAL.drawEvents.CrainWidth));
            e.FillRectangle(blueBrush, new Rectangle(x + 1, y + 1, GLOBAL.drawEvents.CrainWidth - 2, GLOBAL.drawEvents.CrainWidth - 2));
            pictureBox1.Image = bit;
        }

        private void DrawPicEx(int x, int y, Color color)
        {

            SolidBrush blueBrush = new SolidBrush(color);
            Graphics e = Graphics.FromImage(bitbuffer);
            //if(isRedraw){e.Clear(Color.White);}
            e.Clear(pictureBox1.BackColor);
            x = x - 450;
            y = pictureBox2.Height - y - 50;
            e.DrawRectangle(new Pen(Color.DarkBlue, 2.0f), new Rectangle(x, y, GLOBAL.drawEvents.CrainWidth, GLOBAL.drawEvents.CrainWidth));
            e.FillRectangle(blueBrush, new Rectangle(x + 1, y + 1, GLOBAL.drawEvents.CrainWidth - 2, GLOBAL.drawEvents.CrainWidth - 2));
            pictureBox2.Image = bitbuffer;
        }
        private void DrawPicBlank(int x, int y)
        {
            Graphics e = Graphics.FromImage(bitbuffer);
            e.Clear(pictureBox1.BackColor);
            x = x - 450;
            y = pictureBox2.Height - y - 50;
            e.DrawLine(new Pen(Color.DarkBlue), new Point(x, y), new Point(x + GLOBAL.drawEvents.CrainWidth, y));
            pictureBox2.Image = bitbuffer;
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();

            Graphics ex = Graphics.FromImage(bitbuffer);
            ex.Clear(pictureBox1.BackColor);
            pictureBox1.Image = bitbuffer;
            pictureBox2.Image = bitbuffer;
        }
    }
}
